import datetime
from rest_framework.views import APIView
from datetime import date
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from django.utils import timezone

from users.models import User
from orders.models import OrderInfo
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import UserGoodsVisitCountSerializer

ONE_MONTHDAYS = 29


class UserCountView(APIView):
    """User Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        user_total_count = User.objects.all().count()

        return Response ({
            'date': now_date,
            'count': user_total_count
        })


class UserDayIncrementView(APIView):
    """User Day Increment Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        # yestoday_n = now_date - datetime.timedelta(days=1)
        users = User.objects.filter(date_joined__gte=now_date)
        user_day_increment_count = users.count()

        # print(yestoday_n.strftime('%Y-%m-%d %X'))
        # print(user_day_increment_count)
        # print(users)
        return Response({
            'date': now_date,
            'count': user_day_increment_count
        })


class UserDayActiveCountView(APIView):
    """User Day Active Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        # yestoday_n = now_date - datetime.timedelta(days=1)
        users = User.objects.filter(last_login__gte=now_date)
        user_day_active_count = users.count()

        # print(yestoday_n.strftime('%Y-%m-%d %X'))
        # print(user_day_increment_count)
        # print(users)
        return Response({
            'date': now_date,
            'count': user_day_active_count
        })


class UserDayOrdersCountView(APIView):
    """User Day Active Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        # yestoday_n = now_date - datetime.timedelta(days=1)
        orders = OrderInfo.objects.filter(create_time__gte=now_date)
        # orders = User.objects.filter(orderinfo_user_set__create_time__gte='2021-11-13')
        # User.objects.filter(User.OrderInfo_set.all())
        # user = User.objects.get(id=10)
        # print(user.orderinfo_set.all())
        users = set()
        # print(orders)

        for order in orders:
        #     print(order.user.id)
        #     print(order.user.username)
            users.add(order.user)

        # user_day_active_count = len(set(orders.user_id))

        # print(yestoday_n.strftime('%Y-%m-%d %X'))
        # print(user_day_increment_count)
        # print(users)
        return Response({
            'date': now_date,
            'count': len(users)
        })


class UserMonthIncrementView(APIView):
    """User Month Increment Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        begin_date = now_date - datetime.timedelta(days=ONE_MONTHDAYS)
        date_list = []
        # user_sum = 0

        for i in range(ONE_MONTHDAYS):
            index_date = begin_date + datetime.timedelta(days=i)
            next_date = index_date + datetime.timedelta(days=1)
            users = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date)
            count = users.count()
            # user_sum += count

            date_list.append({
                'date': index_date,
                'count': count
                })

        # print(yestoday_n.strftime('%Y-%m-%d %X'))
        # print(user_day_increment_count)
        # print(users)
        return Response(
            date_list
            # {
            # 'ret': ret,
            # 'sum': user_sum
            # }
        )


class UserGoodsVisitCountView(APIView):
    """User Count"""
    permission_classes = [IsAdminUser]

    def get(self, request):
        now_date = date.today()
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)
        ser = UserGoodsVisitCountSerializer(goods, many=True)

        return Response(
            ser.data
        )
